<?php

namespace app\admin\controller;

use app\common\file\ExcelService;
use app\model\WorkHour;
use app\model\OnJob;
use app\model\Staff;
use app\model\Enter;
use app\model\User;
use app\model\Task;
use think\facade\Env;

/**
 *  驿站管理
 */
class PostHouser extends Auth
{

    /**
     * 驿站管理
     *
     * @return void
     */
    public function list()
    {
        $list = Enter::getList();
        success('驿站列表', $list);
    }

    /**
     * 导入待结算金额
     *
     * @return void
     */
    public function importMoney()
    {
        $data = ExcelService::importExcel();
        if (empty($data['data'])) {
            error('数据不能为空');
        }
        $res = Enter::importMoney($data['data']);
        if ($res > 0) {
            success('成功导入' . $res . '条数据');
        } else {
            error('导入失败');
        }
    }

    /**
     * 查看详情
     *
     * @return void
     */
    public function view()
    {
        $id = input('id');
        if (empty($id)) {
            error('缺少参数');
        }
        $info = Enter::getInfo($id);
        success('合伙人信息', $info);
    }

    /**
     * 修改
     *
     * @return void
     */
    public function update()
    {
        $update_data = [
            'shenfen'                => input('shenfen'),
            'enter_sheng'            => input('enter_sheng'),
            'enter_shi'              => input('enter_shi'),
            'enter_xian'             => input('enter_xian'),
            'enter_address_details'  => input('enter_address_details'),
            'boss_id'                => input('boss_id'),
            'name'                   => input('name'),
            'id_num'                 => input('id_num'),
            'phone'                  => input('phone'),
        ];

        $id = input('id');
        if (empty($id)) {
            error('缺少参数');
        }
        array_unique($update_data);

        try {
            Enter::where('id', $id)->update($update_data);
        } catch (\Exception $e) {
            error($e->getMessage());
        }

        success('修改成功');
    }

    /**
     * 冻结、解冻
     *
     * @return void
     */
    public function setStatus()
    {
        $user_id = input('user_id');
        if (!User::where('id', $user_id)->find()) {
            error('没有此用户');
        }
        $status = User::where('id', $user_id)->value('status');
        if ($status == 1) {
            $status = 0;
            $msg = '已解冻';
        } else {
            $status = 1;
            $msg = '已冻结';
        }

        try {
            User::where('id', $user_id)->update(['status' => $status]);
        } catch (\Exception $e) {
            error($e->getMessage());
        }

        success($msg);
    }

    /**
     * 导入工时
     *
     * @return void
     */
    public function importWorkHour()
    {
        $data = ExcelService::importExcel();
        if (empty($data['data'])) {
            error('数据不能为空');
        }
        $res = WorkHour::createDataByExcel($data['data']);
        if ($res > 0) {
            success('成功导入' . $res . '条数据');
        } else {
            error('导入失败');
        }
    }

    /**
     * 工时列表
     *
     * @return void
     */
    public function workHourList()
    {
        $time = input('time');
        if (empty($time)) {
            $time = strtotime(date('Y-m-1', time()));
        } else {
            $time = strtotime(date('Y-m-1', strtotime($time)));
        }

        $list = WorkHour::getList($time);
        success('工时列表', $list);
    }

    /**
     * 修改工时
     *
     * @return void
     */
    public function updateWorkHour()
    {
        $id = input('id');
        $data = [
            'name'         => input('name'),
            'time'         => input('time'),
            'id_num'       => input('id_num'),
            'service_unit' => input('service_unit'),
            'work_hour'    => input('work_hour'),
        ];

        if (empty($id) || empty($data['name']) || empty($data['time']) || empty($data['id_num']) || empty($data['service_unit']) || empty($data['work_hour'])) {
            error('缺少参数');
        }
        $data['time'] = strtotime($data['time']);
        $data['update_time'] = date('Y-m-d H:i:s', time());
        try {
            WorkHour::where('id', $id)->update($data);
            success('修改成功');
        } catch (\Exception $e) {
            error($e->getMessage());
        }
    }

    /**
     * 删除工时
     *
     * @return void
     */
    public function delWorkHour()
    {
        $id = input('id');
        if (empty($id)) {
            error('缺少参数');
        }
        try {
            WorkHour::where('id', $id)->delete();
            success('删除成功');
        } catch (\Exception $e) {
            error($e->getMessage());
        }
    }

    /**
     * 导入离职人员
     *
     * @return void
     */
    public function importLeaveJob()
    {
        $data = ExcelService::importExcel();
        if (empty($data['data'])) {
            error('数据不能为空');
        }
        foreach ($data['data'] as $k => $v) {
            if (empty($v['A']) && empty($v['B']) && empty($v['C']) && empty($v['D']) && empty($v['E'])) {
                unset($data['data'][$k]);
            }
        }
        $res = OnJob::createLeaveJobByExcel($data['data']);
        if ($res > 0) {
            success('成功导入' . $res . '条数据');
        } else {
            error('导入失败');
        }
    }

    /**
     * 离职人员列表
     *
     * @return void
     */
    public function leaveJobList()
    {
        $user_id = input('user_id');
        $time = input('time');
        $list = Staff::leaveJobList($user_id, $time);
        success('离职人员列表', $list);
    }

    /**
     * 修改离职人员
     *
     * @return void
     */
    public function updateLeaveJob()
    {
        $id = input('id');
        $data = [
            'leave_time' => input('leave_time'),
            'cause'      => input('cause')
        ];

        if (empty($id) || empty($data['leave_time']) || empty($data['cause'])) {
            error('缺少参数');
        }
        try {
            OnJob::where('id', $id)->update($data['data']);
            success('修改成功');
        } catch (\Exception $e) {
            error($e->getMessage());
        }
    }

    /**
     * 在职人员列表
     *
     * @return void
     */
    public function onJobList()
    {
        $user_id = input('user_id');
        $time = input('time');
        $list = Staff::getonJobList($user_id, $time);

        $is_download = input('is_download');
        if (!empty($is_download)) {
            if (!empty($user_id)) {
                $staff_id = Staff::where('boss_id', $this->getUserId())->column('id');
                $where[] = ['staff_id', 'in', $staff_id];
            }
            // 确定入职时间
            if (!empty($time)) {
                $y = date('Y', strtotime($time));
                $m = date('m', strtotime($time));
            } else {
                $y = date('Y', time());
                $m = date('m', time());
            }

            $time = mFristAndLast($y, $m);
            $start_time = date('Y-m-d H:i:s', $time['firstday']);
            $end_time = date('Y-m-d H:i:s', $time['lastday']);

            $where[] = ['entry_time', 'between', "{$start_time}, {$end_time}"];

            $list = OnJob::where($where)
                ->field('id, name, id_num, entry_time, task_id')
                ->select()->toArray();
            foreach ($list as $k => $v) {
                $task_info = Task::where('id', $v['task_id'])->find();
                $list[$k]['company_name'] = $task_info['company_name'];
                $list[$k]['address_sheng'] = $task_info['address_sheng'];
                $list[$k]['address_shi'] = $task_info['address_shi'];
                $list[$k]['address_details'] = $task_info['address_details'];
            }
            $path = ExcelService::exportExcel(date('Y-m', $time['firstday']) . '在职人员列表' . date('YmdHis', time()), $list, 2);
            success('在职人员列表下载地址', Env::get('FILE.PATH') . 'exportExcel/' . $path);
        }
        success('在职人员列表', $list);
    }

    /**
     * 待入职人员列表
     *
     * @return void
     */
    public function waitOnJobList()
    {
        $user_id = input('user_id');
        $list = Staff::waitOnJobList($user_id);
        success('待入职人员列表', $list);
    }
}
